package com.wetao.lib_common.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.tstudy.blepenlib.BlePenStreamManager;
import com.wetao.lib_common.ConstantArouter;
import com.wetao.lib_common.Constants;
import com.wetao.lib_common.EventBus.EventBusMessage;
import com.wetao.lib_common.R;
import com.wetao.lib_common.common.ui.Common_LoginActivity;
import com.wetao.lib_common.greendao.DBUtils;
import com.wetao.lib_common.net.cookies.CookiesManager;
import com.wetao.lib_common.view.Dialogue_title;

import org.greenrobot.eventbus.EventBus;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by Hell on 2016/6/24.
 */
public class DialogUtil {
    private static final String TAG = "DialogUtil";

    private boolean mIsReLogin = false;

    /**
     * 静态内部类实现单例
     */
    private static class DialogHolder {
        private static final DialogUtil INSTANCE = new DialogUtil();
    }

    private DialogUtil() {
    }

    public static final DialogUtil getInstance() {
        return DialogHolder.INSTANCE;
    }


    public boolean getmIsReLogin() {
        return mIsReLogin;
    }
    /**
     * 重新登录弹框
     *
     * @param context
     * @param tips
     */
    public void reLoginDialog(final Context context, String tips) {
        try {
            if(mIsReLogin) {
                return;
            }
            EventBus.getDefault().post(new EventBusMessage(EventBusMessage.EVENT_BUS_ACTION_RELOGIN,null));
            mIsReLogin = true;
            Dialog mDialog = new Dialog(context, R.style.ActionSheetDialogStyle2);
            mDialog.setCanceledOnTouchOutside(false);
            mDialog.setCancelable(false);
            mDialog.setContentView(R.layout.dialog_relogin_tip);
            Window window = mDialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            window.setGravity(Gravity.CENTER);
            lp.alpha = 1f; // 透明度
            window.setAttributes(lp);
            mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    mIsReLogin = false;
                }
            });

            mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

                    if (keyCode == KeyEvent.KEYCODE_BACK) {

                        return true;

                    }

                    return false;

                }

            });

            TextView tv_tip = mDialog.findViewById(R.id.tv_board_exit_tip);
            tv_tip.setText(tips);
            Button btn_ok = mDialog.findViewById(R.id.btn_dialog_re_login_ok);
            btn_ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mIsReLogin = false;
                    onConnectionConflict(context);
                    mDialog.dismiss();
                }
            });
            mDialog.show();
        } catch (Exception e) {
            Log.d(TAG, e.toString());
        }
    }


    //被踢下线处理
    private void onConnectionConflict(Context context) {
        DBUtils.getInstance(context).close();
        DBUtils.getInstance(context).clear();


        //清除所有Cookies缓存内容
        CookiesManager.clearAllCookies();
        SPUtils.getInstance().put(Constants.HEAD_XTOKEN, "");
        SPUtils.getInstance().put(Constants.USER_ID_KEY, "");
        EventBus.getDefault().post(new EventBusMessage(EventBusMessage.EVENT_BUS_ACTION_OUTLOGIN, null));
        //SPUtils.getInstance().put(Constants.USER_ID_LIST, "");


        /**
         * 退出登录时通知主程序finish
         */
//        BaseEvent base=new BaseEvent();
//        base.setCode(Constants.EVENT_BUS.USER_UPDATE_OUT_LOGIN);
//        EventBus.getDefault().post(base);

        Intent intent = new Intent();
        intent.setClass(context, Common_LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

    }

}




